<?php

/**
 * Created by JetBrains PhpStorm.
 * User: GMC02
 * Date: 10/9/13
 * Time: 4:02 PM
 * To change this template use File | Settings | File Templates.
 */
class SiteController extends Controller {

    public $layout = '//layouts/main.php';

    public function accessRules() {
        return array();
    }

    public function filters() {
        return array(
            'accessControl',
        );
    }

    public function actions() {
        return array(
            'sitemapxml' => array(
                'class' => 'ext.sitemap.ESitemapXMLAction',
                'importListMethod' => 'getBaseSitePageList',
            ),
        );
    }

    public function actionChangeTheme() {
        $theme = isset($_GET['theme']) ? $_GET['theme'] : 'main';

        if ($theme == 'blue') {
            $cookie = new  CHttpCookie('rvhlayout', '//layouts/blue');
        } elseif ($theme == 'orange') {
            $cookie = new  CHttpCookie('rvhlayout', '//layouts/orange');
        } elseif ($theme == 'green') {
            $cookie = new  CHttpCookie('rvhlayout', '//layouts/green');
        } else {
            $cookie = new  CHttpCookie('rvhlayout', '//layouts/main');
        }

        $cookie->expire = time() + 60 * 60 * 24 * 10; //10 days
        Yii::app()->request->cookies['rvhlayout'] = $cookie;
        $this->redirect(array('site/index'));

    }

    public function  actionIndex() {
        $product = Product::model()->findAll(array(
            'select' => '*',
            'condition' => 'image_id = 0 and (published  = 1 or status = 1)',
            'order' => 'product_id DESC',
            'limit' => 5
        ));

        #region SEO
        Yii::app()->clientScript->registerLinkTag('canonical', null, TreTodayCommon::getCurrentUrl());
        Yii::app()->clientScript->registerMetaTag(Yii::app()->params['meta_description'], 'description');
        Yii::app()->clientScript->registerMetaTag(Yii::app()->params['meta_keywords'], 'keywords');
        $this->pageTitle = 'Rao vặt hot | Mua bán | Rao vặt | Shopping';
        #endregion SEO

        $this->render('index', array('products' => $product));
    }

    public function beforeaction() {
        if (!isset(Yii::app()->request->cookies['rvhlayout'])) {
            $cookie = new  CHttpCookie('rvhlayout', '//layouts/main');
            $cookie->expire = time() + 60 * 60 * 24 * 10; //10 days
            Yii::app()->request->cookies['rvhlayout'] = $cookie;
        }
        $cookie = Yii::app()->request->cookies['rvhlayout'];
        $this->layout = $cookie->value;

        return true;
    }

    public function actionError() { $this->redirect(TreTodayUrl::home()); }

    public function getBaseSitePageList() {
        $list = array(
            array(
                'loc' => Yii::app()->createAbsoluteUrl('/'),
                'frequency' => 'daily',
                'priority' => '1',
            ),

        );;
        $items = Category::model()->findAll(array(
            'select' => '*',
            'condition' => 'parent_id >0'
        ));
        foreach ($items as $item) {
            $arr = array(
                'loc' => TreTodayUrl::getCategoryUrl($item->cat_id, $item->cat_name),
                'frequency' => 'weekly',
                'priority' => '0.4',
            );
            array_push($list, $arr);
        }
        return $list;
    }

}